package text;
import mathsys2.Arithmetic;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
 * 算式类测试：验证所有功能和异常场景
 */
public class ArithmeticTest {

    // 正常加法测试
    @Test
    public void testAddition() {
        Arithmetic add = new Arithmetic(3, 5, '+');
        assertEquals(8, add.getResult());
        assertEquals("3 + 5 = ?", add.toString());
    }

    // 正常减法测试
    @Test
    public void testSubtraction() {
        Arithmetic sub = new Arithmetic(10, 4, '-');
        assertEquals(6, sub.getResult());
        assertEquals("10 - 4 = ?", sub.toString());
    }

    // 加法边界测试（和为100）
    @Test
    public void testAdditionMax() {
        Arithmetic add = new Arithmetic(50, 50, '+');
        assertEquals(100, add.getResult());
    }

    // 减法边界测试（差为0）
    @Test
    public void testSubtractionZero() {
        Arithmetic sub = new Arithmetic(5, 5, '-');
        assertEquals(0, sub.getResult());
    }

    // 加法交换律去重测试
    @Test
    public void testAdditionEquality() {
        Arithmetic add1 = new Arithmetic(2, 7, '+');
        Arithmetic add2 = new Arithmetic(7, 2, '+');
        assertTrue(add1.equals(add2));
        assertEquals(add1.hashCode(), add2.hashCode());
    }

    /* 减法非交换测试
    @Test
    public void testSubtractionEquality() {
        Arithmetic sub1 = new Arithmetic(9, 3, '-');
        Arithmetic sub2 = new Arithmetic(3, 9, '-');
        assertFalse(sub1.equals(sub2));
    }

     */

    // 无效运算符异常测试
    @Test
    public void testInvalidOperator() {
        // 测试运算符 '*'
        IllegalArgumentException e1 = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(2, 3, '*'),
                "未对 '*' 抛出异常"
        );
        assertEquals("仅支持 '+' 和 '-' 运算符", e1.getMessage(), "* 异常信息错误");

        // 测试运算符 '/'
        IllegalArgumentException e2 = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(6, 2, '/'),
                "未对 '/' 抛出异常"
        );
        assertEquals("仅支持 '+' 和 '-' 运算符", e2.getMessage(), "/ 异常信息错误");
    }

    // 加法结果超100异常测试
    @Test
    public void testAdditionOver100() {
        IllegalArgumentException e = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(60, 50, '+'),
                "未对加法超100抛出异常"
        );
        assertEquals("加法结果超过100，不符合约束", e.getMessage());
    }

    // 减法结果为负异常测试
    @Test
    public void testSubtractionNegative() {
        IllegalArgumentException e = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(3, 9, '-'),  // 3-9为负数
                "未对减法负数抛出异常"
        );
        assertEquals("减法结果为负数，不符合约束", e.getMessage());
    }

    // 运算数越界异常测试
    @Test
    public void testNumberOutOfRange() {
        // 负数运算数
        IllegalArgumentException e1 = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(-1, 5, '+'),
                "未对负数运算数抛出异常"
        );
        assertEquals("运算数必须在0-100之间", e1.getMessage());

        // 超100运算数
        IllegalArgumentException e2 = assertThrows(
                IllegalArgumentException.class,
                () -> new Arithmetic(50, 101, '-'),
                "未对超100运算数抛出异常"
        );
        assertEquals("运算数必须在0-100之间", e2.getMessage());
    }
}